package com.zjw.zy.bean;

import android.os.Parcel;
import android.os.Parcelable;

import androidx.annotation.NonNull;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.Ignore;
import androidx.room.PrimaryKey;

import com.zjw.zy.smi.ICopy;
import com.zjw.zy.utils.TimeUtils;

import java.util.Objects;

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/11/1 17:10
 * 提醒bean
 */
@Entity(tableName = "tips")
public class Tip implements ICopy<Tip>, Parcelable {

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id", typeAffinity = ColumnInfo.INTEGER)
    public int id;

    @ColumnInfo(name = "tip", typeAffinity = ColumnInfo.TEXT)
    public String tip;

    @ColumnInfo(name = "time", typeAffinity = ColumnInfo.INTEGER)
    public long time;

    @ColumnInfo(name = "isDone", typeAffinity = ColumnInfo.INTEGER)
    public boolean isDone = false;

    @ColumnInfo(name = "tag", typeAffinity = ColumnInfo.TEXT)
    public String tag = "";

    @Ignore
    public Tip() {
    }

    public Tip(int id, String tip, long time, boolean isDone, String tag) {
        this.id = id;
        this.tip = tip;
        this.time = time;
        this.isDone = isDone;
        this.tag = tag;
    }


    @Ignore
    protected Tip(Parcel in) {
        id = in.readInt();
        tip = in.readString();
        time = in.readLong();
        isDone = in.readByte() != 0;
        tag = in.readString();
    }

    public static final Creator<Tip> CREATOR = new Creator<Tip>() {
        @Override
        public Tip createFromParcel(Parcel in) {
            return new Tip(in);
        }

        @Override
        public Tip[] newArray(int size) {
            return new Tip[size];
        }
    };

    public String getTip() {
        return tip;
    }

    public long getTime() {
        return time;
    }

    public void setDone(boolean done) {
        isDone = done;
    }


    @NonNull
    @Override
    public Tip copy() {
        return new Tip(id, tip, time, isDone, tag);
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(id);
        dest.writeString(tip);
        dest.writeLong(time);
        dest.writeByte((byte) (isDone ? 1 : 0));
        dest.writeString(tag);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Tip tip1 = (Tip) o;
        return id == tip1.id && time == tip1.time && isDone == tip1.isDone
                && Objects.equals(tip, tip1.tip) && Objects.equals(tag, tip1.tag);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, tip, time, isDone, tag);
    }

    public boolean isPlan() {
        return !isDone &&
                (TimeUtils.isFuture(TimeUtils.getTimeBean(time)) || TimeUtils.isToday(
                        TimeUtils.getTimeBean(time)
                ));
    }

    public boolean isToday() {
        return !isDone && TimeUtils.isToday(
                TimeUtils.getTimeBean(time)
        );
    }

    //过期任务
    public boolean isPast() {
        return !isDone && TimeUtils.isPast(TimeUtils.getTimeBean(time));
    }

    @Override
    public String toString() {
        return "Tip{" +
                "id=" + id +
                ", tip='" + tip + '\'' +
                ", time=" + time +
                ", isDone=" + isDone +
                ", tag='" + tag + '\'' +
                '}';
    }
}
